﻿using AutoMapper;
using Common.Property;
using Common.Utility;
using DTO;
using DTO.DTO.Sys.Module;
using DTO.ViewModel;
using IServices.Sys;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Web.Controllers;

namespace Web.Areas.Sys.Controllers
{
    public class ModuleController : BaseController
    {
        private readonly IRoleService _service;

        public ModuleController(IHttpContextAccessor httpContextAccessor, IMapper mapper, IRoleService service) : base(httpContextAccessor, mapper)
        {
            _service = service;
        }
        // GET: Sys/Module
        public IActionResult Index()
        {
            ViewBag.Status = EnumHelper.GetNVCFromEnumString(typeof(E_Status));
            return View();
        }

        public async Task<ResultList> List(ModuleQueryDTO dto)
        {
            var result = await _service.GetModulePageList(dto);
            var map = _mapper.Map<List<ModuleListDTO>>(result.Item1);
            return ResultList.Success(count: result.Item2, data: map);
        }

        public async Task<Result> Del(string id)
        {
            return await _service.DeleteModule(id, OperatorInfo) ? Result.Success("删除成功") : Result.Failed(-1, "删除失败");
        }

        public async Task<Result> Delist(List<string> ids)
        {
            return await _service.DeleteBatchModule(ids, OperatorInfo) ? Result.Success("删除成功") : Result.Failed(-1, "删除失败");
        }

        public async Task<IActionResult> Edit(string id)
        {
            ViewBag.Types = EnumHelper.GetNVCFromEnumString(typeof(E_ModuleType));
            ViewBag.ModuleList = await Task.Run(()=> _service.GetRecursionModuleList("", "", "", ""));

            if (string.IsNullOrWhiteSpace(id))
            {
                var initModel = new ModuleDetailDTO();
                return View(initModel);
            }
            else
            {
                var model = await _service.GetModuleById(id);
                var modelDTO = _mapper.Map<ModuleDetailDTO>(model);
                return View(modelDTO);
            }
        }

        public async Task<Result> Save(ModuleDetailDTO model)
        {
            return await _service.SaveModule(model, OperatorInfo);
        }

        public IActionResult GetJson()
        {
            // 获取所有资源
            var list = _service.GetRecursionModuleList("", "", E_Status.Enabled.ToString(), "");

            // 树默认根节点
            var tree = new List<TreeModel>
            {
                new TreeModel
                {
                    PId = "-1",
                    Id = "0",
                    Name = "所有资源",
                    Open = true
                }
            };

            // 向树中添加资源节点
            tree.AddRange(list.Select(item => new TreeModel
            {
                PId = string.IsNullOrWhiteSpace(item.ParentId) ? "0" : item.ParentId,
                Id = item.Id,
                Name = item.Name,
                Open = false
            }));

            // 返回数据
            return Json(tree);
        }

    }
}